package com.xuecheng.manage_cms.controller;

import com.xuecheng.api.cms.CmsConfigControllerApi;
import com.xuecheng.api.cms.CmsPageControllerApi;
import com.xuecheng.framework.domain.cms.CmsConfig;
import com.xuecheng.framework.domain.cms.CmsPage;
import com.xuecheng.framework.domain.cms.request.QueryPageRequest;
import com.xuecheng.framework.domain.cms.response.CmsPageResult;
import com.xuecheng.framework.domain.cms.response.CmsPostPageResult;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.QueryResult;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.manage_cms.service.CmsConfigService;
import com.xuecheng.manage_cms.service.PageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
//@RestController相当于@Controller+@ResponseBody
@RestController
@RequestMapping("/cms/page")
public class CmsPageController implements CmsPageControllerApi, CmsConfigControllerApi {
    @Autowired
    private PageService pageService;
    @Autowired
    private CmsConfigService cmsConfigService;
    //分页查询页面
    @Override
    @GetMapping("/list/{page}/{size}")
    public QueryResponseResult findList(@PathVariable("page") int page,@PathVariable("size") int size, QueryPageRequest queryPageRequest) {
        /*QueryResult<CmsPage> queryResult = new QueryResult<>();
        ArrayList<CmsPage> list = new ArrayList<>();
        CmsPage cmsPage = new CmsPage();
        cmsPage.setPageName("测试页面");
        list.add(cmsPage);
        queryResult.setList(list);
        queryResult.setTotal(1);
        QueryResponseResult queryResponseResult = new QueryResponseResult(CommonCode.SUCCESS,queryResult);
        return queryResponseResult;*/
        queryPageRequest=null;
            return pageService.findList(page, size, queryPageRequest);
    }
    //新增页面
    @Override
    @PostMapping("/add")
    public CmsPageResult add(@RequestBody CmsPage cmsPage) {
        CmsPageResult cmsPageResult = pageService.add(cmsPage);
        return cmsPageResult;
    }
    //通过id查找页面
    @Override
    @GetMapping("/findById/{pageId}")
    public CmsPage findById(@PathVariable("pageId") String pageId) {
        CmsPage cmsPage =pageService.findById(pageId);
        return cmsPage;
    }
    //修改页面
    @Override
    @PutMapping("/updateById/{pageId}")
    public CmsPageResult updatePage(@PathVariable("pageId") String pageId, @RequestBody CmsPage cmsPage) {
        CmsPageResult cmsPageResult =pageService.updatePage(pageId,cmsPage);
        return cmsPageResult;
    }
    //删除页面
    @Override
    @DeleteMapping("/deletePage/{pageId}")
    public ResponseResult deletePage(@PathVariable("pageId") String pageId) {
        ResponseResult responseResult=pageService.deletePage(pageId);
        return responseResult;
    }
    //页面发布
    @Override
    @PostMapping("/postPage/{pageId}")
    public ResponseResult post(@PathVariable("pageId") String pageId) {
        return  pageService.postPage(pageId);
    }
    //保存页面
    @Override
    @PostMapping("/save")
    public CmsPageResult save(@RequestBody CmsPage cmsPage) {
        return pageService.save(cmsPage);
    }
    //一键页面发布
    @Override
    @PostMapping("/postPageQuick")
    public CmsPostPageResult postPageQuick(@RequestBody CmsPage cmsPage) {
        return pageService.postPageQuick(cmsPage);
    }

    //根据Id获取模板
    @Override
    @GetMapping("/getmodel/{id}")
    public CmsConfig getmodel(@PathVariable("id") String id) {

        return cmsConfigService.getCmsConfig(id);
    }
}
